package gambar;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Amira Herwindyani
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class dlgambar {

    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/image";
        Connection conn = DriverManager.getConnection(url, "root", "");

        String sql = "SELECT IMG_ID, IMG FROM image ";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet resultSet = stmt.executeQuery();
        while (resultSet.next()) {
            String IMG_ID = resultSet.getString(1);
            File image = new File("A:\\java.gif");
            FileOutputStream fos = new FileOutputStream(image);

            byte[] buffer = new byte[1];
            InputStream is = resultSet.getBinaryStream(2);
            while (is.read(buffer) > 0) {
                fos.write(buffer);
            }
            fos.close();
        }
        conn.close();
    }
}
